home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / bison / files.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-12-30  |  6.9 KB  |  289 lines

  1. /* Open and close files for bison,
  2.    Copyright (C) 1984, 1986 Bob Corbett and Free Software Foundation, Inc.
  3.  
  4. BISON is distributed in the hope that it will be useful, but WITHOUT ANY
  5. WARRANTY.  No author or distributor accepts responsibility to anyone
  6. for the consequences of using it or for whether it serves any
  7. particular purpose or works at all, unless he says so in writing.
  8. Refer to the BISON General Public License for full details.
  9.  
  10. Everyone is granted permission to copy, modify and redistribute BISON,
  11. but only under the conditions described in the BISON General Public
  12. License.  A copy of this license is supposed to have been given to you
  13. along with BISON so you can know your rights and responsibilities.  It
  14. should be in a file named COPYING.  Among other things, the copyright
  15. notice and this notice must be preserved on all copies.
  16.  
  17.  In other words, you are welcome to use, share and improve this program.
  18.  You are forbidden to forbid anyone else to use, share and improve
  19.  what you give them.   Help stamp out software-hoarding!  */
  20.  
  21. #ifdef VMS
  22. #include <ssdef.h>
  23. #define unlink delete
  24. #define XPFILE "GNU_BISON:[000000]BISON.SIMPLE"
  25. #define XPFILE1 "GNU_BISON:[000000]BISON.HAIRY"
  26. #endif
  27.  
  28. #include <stdio.h>
  29. #include "files.h"
  30. #include "new.h"
  31. #include "gram.h"
  32.  
  33. FILE *finput = NULL;
  34. FILE *foutput = NULL;
  35. FILE *fdefines = NULL;
  36. FILE *ftable = NULL;
  37. FILE *fattrs = NULL;
  38. FILE *fguard = NULL;
  39. FILE *faction = NULL;
  40. FILE *fparser = NULL;
  41.  
  42. /* File name specified with -o for the output file, or 0 if no -o.  */
  43. char *spec_outfile;
  44.  
  45. char *infile;
  46. char *outfile;
  47. char *defsfile;
  48. char *tabfile;
  49. char *attrsfile;
  50. char *guardfile;
  51. char *actfile;
  52. char *tmpattrsfile;
  53. char *tmptabfile;
  54.  
  55. char    *mktemp();    /* So the compiler won't complain */
  56. FILE    *tryopen();    /* This might be a good idea */
  57.  
  58. extern int verboseflag;
  59. extern int definesflag;
  60. int fixed_outfiles = 0;
  61.  
  62.  
  63. char*
  64. stringappend(string1, end1, string2)
  65. char *string1;
  66. int end1;
  67. char *string2;
  68. {
  69.   register char *ostring;
  70.   register char *cp, *cp1;
  71.   register int i;
  72.  
  73.   cp = string2;  i = 0;
  74.   while (*cp++) i++;
  75.  
  76.   ostring = NEW2(i+end1+1, char);
  77.  
  78.   cp = ostring;
  79.   cp1 = string1;
  80.   for (i = 0; i < end1; i++)
  81.     *cp++ = *cp1++;
  82.  
  83.   cp1 = string2;
  84.   while (*cp++ = *cp1++) ;
  85.  
  86.   return ostring;
  87. }
  88.  
  89.  
  90. /* JF this has been hacked to death.  Nowaday it sets up the file names for
  91.    the output files, and opens the tmp files and the parser */
  92. openfiles()
  93. {
  94.   char *name_base;
  95.   register char *cp;
  96.   char *filename;
  97.   int base_length;
  98.   int short_base_length;
  99.  
  100. #ifdef VMS
  101.   char *tmp_base = "sys$scratch:b_";
  102. #else
  103.   char *tmp_base = "/tmp/b.";
  104. #endif
  105.   int tmp_len = strlen (tmp_base);
  106.  
  107.   if (spec_outfile)
  108.     {
  109.       /* -o was specified.  The precise -o name will be used for ftable.
  110.      For other output files, remove the ".c" or ".tab.c" suffix.  */
  111.       name_base = spec_outfile;
  112.       /* BASE_LENGTH includes ".tab" but not ".c".  */
  113.       base_length = strlen (name_base);
  114.       if (!strcmp (name_base + base_length - 2, ".c"))
  115.     base_length -= 2;
  116.       /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c".  */
  117.       short_base_length = base_length;
  118.       if (!strcmp (name_base + short_base_length - 4, ".tab"))
  119.     short_base_length -= 4;
  120.       else if (!strcmp (name_base + short_base_length - 4, "_tab"))
  121.     short_base_length -= 4;
  122.     }
  123.   else
  124.     {
  125.       /* -o was not specified; compute output file name from input
  126.      or use y.tab.c, etc., if -y was specified.  */
  127.  
  128.       name_base = fixed_outfiles ? "y.y" : infile;
  129.  
  130.       /* Discard any directory names from the input file name
  131.      to make the base of the output.  */
  132.       cp = name_base;
  133.       while (*cp)
  134.     {
  135.       if (*cp == '/')
  136.         name_base = cp+1;
  137.       cp++;
  138.     }
  139.  
  140.       /* BASE_LENGTH gets length of NAME_BASE, sans ".y" suffix if any.  */
  141.  
  142.       base_length = strlen (name_base);
  143.       if (!strcmp (name_base + base_length - 2, ".y"))
  144.     base_length -= 2;
  145.       short_base_length = base_length;
  146.  
  147. #ifdef VMS
  148.       name_base = stringappend(name_base, short_base_length, "_tab");
  149. #else
  150.       name_base = stringappend(name_base, short_base_length, ".tab");
  151. #endif
  152.       base_length = short_base_length + 4;
  153.     }
  154.  
  155.   finput = tryopen(infile, "r");
  156.  
  157.   filename = (char *) getenv ("BISON_SIMPLE");
  158.   fparser = tryopen(filename ? filename : PFILE, "r");
  159.  
  160.   if (verboseflag)
  161.     {
  162.       outfile = stringappend(name_base, short_base_length, ".output");
  163.       foutput = tryopen(outfile, "w");
  164.     }
  165.  
  166.   if (definesflag)
  167.     {
  168.       defsfile = stringappend(name_base, base_length, ".h");
  169.       fdefines = tryopen(defsfile, "w");
  170.     }
  171.  
  172.   actfile = mktemp(stringappend(tmp_base, tmp_len, "act.XXXXXX"));
  173.   faction = tryopen(actfile, "w+");
  174.   unlink(actfile);
  175.  
  176.   tmpattrsfile = mktemp(stringappend(tmp_base, tmp_len, "attrs.XXXXXX"));
  177.   fattrs = tryopen(tmpattrsfile,"w+");
  178.   unlink(tmpattrsfile);
  179.  
  180.   tmptabfile = mktemp(stringappend(tmp_base, tmp_len, "tab.XXXXXX"));
  181.   ftable = tryopen(tmptabfile, "w+");
  182.   unlink(tmptabfile);
  183.  
  184.     /* These are opened by `done' or `open_extra_files', if at all */
  185.   if (spec_outfile)
  186.     tabfile = spec_outfile;
  187.   else
  188.     tabfile = stringappend(name_base, base_length, ".c");
  189.  
  190. #ifdef VMS
  191.   attrsfile = stringappend(name_base, short_base_length, "_stype.h");
  192.   guardfile = stringappend(name_base, short_base_length, "_guard.c");
  193. #else
  194.   attrsfile = stringappend(name_base, short_base_length, ".stype.h");
  195.   guardfile = stringappend(name_base, short_base_length, ".guard.c");
  196. #endif
  197. }
  198.  
  199.  
  200.  
  201. /* open the output files needed only for the semantic parser.
  202. This is done when %semantic_parser is seen in the declarations section.  */
  203. open_extra_files()
  204. {
  205.   FILE *ftmp;
  206.   int c;
  207.   char *filename;
  208.         /* JF change open parser file */
  209.   fclose(fparser);
  210.   filename = (char *) getenv ("BISON_HAIRY");
  211.   fparser= tryopen(filename ? filename : PFILE1, "r");
  212.  
  213.         /* JF change from inline attrs file to separate one */
  214.   ftmp = tryopen(attrsfile, "w");
  215.   rewind(fattrs);
  216.   while((c=getc(fattrs))!=EOF)    /* Thank god for buffering */
  217.     putc(c,ftmp);
  218.   fclose(fattrs);
  219.   fattrs=ftmp;
  220.  
  221.   fguard = tryopen(guardfile, "w");
  222.  
  223. }
  224.  
  225.     /* JF to make file opening easier.  This func tries to open file
  226.        NAME with mode MODE, and prints an error message if it fails. */
  227. FILE *
  228. tryopen(name, mode)
  229. char *name;
  230. char *mode;
  231. {
  232.   FILE    *ptr;
  233.  
  234.   ptr = fopen(name, mode);
  235.   if (ptr == NULL)
  236.     {
  237.       fprintf(stderr, "bison: ");
  238.       perror(name);
  239.       done(2);
  240.     }
  241.   return ptr;
  242. }
  243.  
  244. done(k)
  245. int k;
  246. {
  247.   if (faction)
  248.     fclose(faction);
  249.  
  250.   if (fattrs)
  251.     fclose(fattrs);
  252.  
  253.   if (fguard)
  254.     fclose(fguard);
  255.  
  256.   if (finput)
  257.     fclose(finput);
  258.  
  259.   if (fparser)
  260.     fclose(fparser);
  261.  
  262.   if (foutput)
  263.     fclose(foutput);
  264.  
  265.     /* JF write out the output file */
  266.   if (k == 0 && ftable)
  267.     {
  268.       FILE *ftmp;
  269.       register int c;
  270.  
  271.       ftmp=tryopen(tabfile, "w");
  272.       rewind(ftable);
  273.       while((c=getc(ftable)) != EOF)
  274.         putc(c,ftmp);
  275.       fclose(ftmp);
  276.       fclose(ftable);
  277.     }
  278.  
  279. #ifdef VMS
  280.   delete(actfile);
  281.   delete(tmpattrsfile);
  282.   delete(tmptabfile);
  283.   if (k==0) sys$exit(SS$_NORMAL);
  284.   sys$exit(SS$_ABORT);
  285. #else
  286.   exit(k);
  287. #endif
  288. }
  289.